package com.doit.doit26;

import com.doit.beans.Emp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class EmpQuery {

  public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //注册驱动
    Class.forName("com.mysql.jdbc.Driver");
    //获取连接
    Connection conn = DriverManager
        .getConnection("jdbc:mysql://127.0.0.1:3306/lvxiaolong", "root", "123456");
    //创建执行sql的对象
    Statement sta = conn.createStatement();
    //sql语句
    String sql = "select * from t_emp";
    //执行sql
    ResultSet resultSet = sta.executeQuery(sql);
    //循环取值
    List<Emp> empList = new ArrayList<Emp>();
    while (resultSet.next()) {
      int eid = resultSet.getInt(1);
      String name = resultSet.getString(2);
      String sex = resultSet.getString(3);
      int idNo = resultSet.getInt(4);
      int did = resultSet.getInt(5);
      Emp emp = new Emp(eid, name, sex, idNo, did);
      empList.add(emp);
    }
    resultSet.close();
    sta.close();
    conn.close();
    for (Emp e : empList) {
      System.out.println(e);
    }
  }

}
